package com.brokendust.client.api.module.text;

import com.brokendust.client.api.util.LocationUtil;
import com.brokendust.client.config.module.text.TextEntityJson;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Vector3f;

import java.util.List;

public class TextEntity {
    private FontRenderer fontRenderer;
    private String uuidString;
    private float scale;
    private double locationHeight;
    private double locationAngle;
    private double locationDistance;
    private int rowHeight;
    private int align;
    private List<String> textList;
    private float rotateX;
    private float rotateY;
    private float rotateZ;
    private int autoRotationX;
    private int autoRotationY;
    private int autoRotationZ;
    private float shortFontWidth;
    private float longerFontWidth;

    public TextEntity(TextEntityJson textEntityJson) {
        this.shortFontWidth = 100.0f;
        this.fontRenderer = Minecraft.getInstance().font;
        this.uuidString = textEntityJson.getUuidString();
        this.scale = textEntityJson.getScale();
        this.locationHeight = textEntityJson.getLocationHeight();
        this.locationAngle = textEntityJson.getLocationAngle();
        this.locationDistance = textEntityJson.getLocationDistance();
        this.rowHeight = textEntityJson.getRowHeight();
        this.align = textEntityJson.getAlign();
        this.textList = textEntityJson.getTextList();
        this.rotateX = textEntityJson.getRotateX();
        this.rotateY = textEntityJson.getRotateY();
        this.rotateZ = textEntityJson.getRotateZ();
        this.autoRotationX = textEntityJson.getAutoRotationX();
        this.autoRotationY = textEntityJson.getAutoRotationY();
        this.autoRotationZ = textEntityJson.getAutoRotationZ();
        for (String text : this.textList) {
            int fn = this.fontRenderer.width(text);
            if (fn > this.longerFontWidth) {
                this.longerFontWidth = (float) fn;
            }
            if (fn < this.shortFontWidth) {
                this.shortFontWidth = (float) fn;
            }
        }
    }

    public void renderText(PlayerEntity player, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn) {
        String playerUUIDString = player.getStringUUID();
        if (!this.uuidString.equals(playerUUIDString)) {
            return;
        }
        if (this.rotateX >= 360.0f) {
            this.rotateX = 0.0f;
        }
        if (this.rotateY >= 360.0f) {
            this.rotateY = 0.0f;
        }
        if (this.rotateZ >= 360.0f) {
            this.rotateZ = 0.0f;
        }
        float fontAngle = player.getRotationVector().y % 360.0f;
        double lookAngle = (player.getRotationVector().y - 90.0f + this.locationAngle) % 360.0;
        double[] lookAngleLocation = LocationUtil.rotateY(lookAngle, new double[]{0.0, 0.0, 0.0}, this.locationDistance);
        for (int i = 0; i < this.textList.size(); ++i) {
            double d = i * this.rowHeight + this.locationHeight * -1.0;
            matrixStackIn.pushPose();
            matrixStackIn.scale(-this.scale, -this.scale, -this.scale);
            matrixStackIn.translate(lookAngleLocation[0], d, lookAngleLocation[2] * -1.0);
            if (this.rotateX > 0.0f) {
                matrixStackIn.mulPose(Vector3f.XP.rotationDegrees(this.rotateX));
            } else {
                matrixStackIn.mulPose(Vector3f.XN.rotationDegrees(this.rotateX * -1.0f));
            }
            if (this.rotateY > 0.0f) {
                matrixStackIn.mulPose(Vector3f.YP.rotationDegrees(this.rotateY));
            } else {
                matrixStackIn.mulPose(Vector3f.YN.rotationDegrees(this.rotateY * -1.0f));
            }
            if (this.rotateZ > 0.0f) {
                matrixStackIn.mulPose(Vector3f.ZP.rotationDegrees(this.rotateZ));
            } else {
                matrixStackIn.mulPose(Vector3f.ZN.rotationDegrees(this.rotateZ * -1.0f));
            }
            if (this.rotateX == 0.0f && this.rotateY == 0.0f && this.rotateZ == 0.0f) {
                matrixStackIn.mulPose(Vector3f.YN.rotationDegrees(fontAngle));
            }
            Matrix4f matrix4f = matrixStackIn.last().pose();
            String text = this.textList.get(i);
            float fontWidth = (float) this.fontRenderer.width(text);
            float offset = -this.longerFontWidth / 2.0f;
            if (this.align == 2) {
                offset = -fontWidth / 2.0f;
            }
            if (this.align == 3) {
                offset = this.shortFontWidth / 2.0f;
            }
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            this.fontRenderer.drawInBatch(text, offset, 0.0f, 553648127, false, matrix4f, bufferIn, false, 0, 15728640);
            matrixStackIn.popPose();
        }
        if (this.autoRotationX > 0) {
            this.rotateX += this.autoRotationX;
        }
        if (this.autoRotationY > 0) {
            this.rotateY += this.autoRotationY;
        }
        if (this.autoRotationZ > 0) {
            this.rotateZ += this.autoRotationZ;
        }
    }
}
